﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CuaHangDiaNhac
{
    public partial class frmDiaNhac : DevComponents.DotNetBar.Office2007Form
    {
        StoreCDDataContext dt;
        public frmDiaNhac()
        {
            InitializeComponent();
            dt = new StoreCDDataContext();
            XuLyDataGridView.CotCheckBox(dgrDanhSach);
        }
        List<int> RowSelect = new List<int>();

        private void frmDiaNhac_Load(object sender, EventArgs e)
        {
            lblThongBao.Text = "";
            XuLy.HienThiTienTrinh();
            GetAll();
            cbLoaiDia.DataSource = dt.LoaiDia_SelectAll();
            cbLoaiDia.DisplayMember = "TenLoai";
            cbLoaiDia.ValueMember = "MaLoai";
            cbNhaCungCap.DataSource = dt.NhaCungCap_SelectAll();
            cbNhaCungCap.DisplayMember = "TenNCC";
            cbNhaCungCap.ValueMember = "MaNCC";
            XuLy.HideTienTrinh();
            lblThongBao.Text = "";
            lblThongbao2.Text = "";
        }
        void GetAll()
        {
            dgrDanhSach.DataSource = dt.DiaNhac_SelectAll();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (BatLoi.TextBoxEmpty(txtTenDia)
    || BatLoi.ComboEmpty(cbNhaCungCap)
    || BatLoi.ComboEmpty(cbLoaiDia))
            {
                BatLoi.ThongBao("Vui lòng xem lại các trường dữ liệu");
                return;
            }
            if (BatLoi.BatloiGia(txtGiaNhap.Text, txtGiaBan.Text))
                return;
            if (BatLoi.BatloiNgayNhap(dttNgayNhap.Value))
                return;
            if (TonTai(txtTenDia.Text))
            {
                lblThongBao.Text = "Tên đĩa đã tồn tại.";
                return;
            }
            if (ConvertType.ToInt(txtGiaBan.Text) < ConvertType.ToInt(txtGiaNhap.Text))
            {
                lblThongbao2.Text = "Đơn Gía Bán không hợp lệ";
                return;
            }
            
            dt.DiaNhac_Insert(txtTenDia.Text, Convert.ToInt32(cbLoaiDia.SelectedValue), Convert.ToInt32(txtSoLuong.Text), Convert.ToInt32(cbNhaCungCap.SelectedValue), Convert.ToDecimal(txtGiaNhap.Text), Convert.ToDecimal(txtGiaBan.Text), dttNgayNhap.Value);
            dt.SubmitChanges();
            GetAll();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (dgrDanhSach.SelectedRows.Count < 1)
                MessageBox.Show("Xin Vui Lòng Chọn Nội dung cần Sửa");
            else
            {
                dt.DiaNhac_Update(Convert.ToInt32(txtMaDia.Text), txtTenDia.Text, Convert.ToInt32(cbLoaiDia.SelectedValue), Convert.ToInt32(txtSoLuong.Text), Convert.ToInt32(cbNhaCungCap.SelectedValue), Convert.ToDecimal(txtGiaNhap.Text), Convert.ToDecimal(txtGiaBan.Text), dttNgayNhap.Value);
                dt.SubmitChanges();
                GetAll();
            }

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(dgrDanhSach.CurrentRow.Cells[0].Value.ToString());
            dt.DiaNhac_Delete(a);
            dt.SubmitChanges();
            GetAll();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (int i in RowSelect)
            {
                try
                {
                    int a = Convert.ToInt32(dgrDanhSach.Rows[i].Cells["MaDia"].Value.ToString());
                    dt.DiaNhac_Delete(a);
                    dt.SubmitChanges();
                }
                catch (Exception)
                {

                }
            }
            GetAll();
            Showthem(false);
        }
        void Showthem(bool them)
        {
            btnXoa.Enabled = btnCapNhat.Enabled = !them;
            btnThem1.Enabled = them;
        }
        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            XuLyDataGridView.CheckRow(dgrDanhSach, RowSelect, e);
            hang = dgrDanhSach.CurrentRow;
            GetData();
            Showthem(false);
        }

        private void dataGridViewX1_DataSourceChanged(object sender, EventArgs e)
        {
            RowSelect.Clear();
        }

        private void btnCapNhat_Click_1(object sender, EventArgs e)
        {
            if (BatLoi.TextBoxEmpty(txtTenDia)
    || BatLoi.ComboEmpty(cbNhaCungCap)
    || BatLoi.ComboEmpty(cbLoaiDia))
            {
                BatLoi.ThongBao("Vui lòng xem lại các trường dữ liệu");
                return;
            }
            if (dgrDanhSach.SelectedRows.Count < 1)
                MessageBox.Show("Xin Vui Lòng Chọn Nội dung cần Sửa");
            else
            {
                dt.DiaNhac_Update(Convert.ToInt32(txtMaDia.Text), txtTenDia.Text, Convert.ToInt32(cbLoaiDia.SelectedValue), Convert.ToInt32(txtSoLuong.Text), Convert.ToInt32(cbNhaCungCap.SelectedValue), Convert.ToDecimal(txtGiaNhap.Text), Convert.ToDecimal(txtGiaBan.Text), dttNgayNhap.Value);
                
                dt.SubmitChanges();
                GetAll();
            }

        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (cbChoose.SelectedIndex == 0)
                dgrDanhSach.DataSource = dt.DiaNhac_SearchById(txtSearch.Text);
            else
                dgrDanhSach.DataSource = dt.DiaNhac_SearchByName(txtSearch.Text);
        }

        DataGridViewRow hang;

        bool TonTai(String s)
        {
            return (from d in dt.vwDiaNhacs
                    where d.TenDia == s
                    select d).Count() > 0;
        }


        private void btnXoa_Click(object sender, EventArgs e)
        {

        }

        void GetData()
        {
            if (hang != null)
            {
                txtMaDia.Text = hang.Cells["MaDia"].Value.ToString();
                lblThongBao.Text = "";
                txtTenDia.Text = hang.Cells["TenDia"].Value.ToString();
                cbLoaiDia.SelectedValue = hang.Cells["LoaiDia"].Value;
                txtSoLuong.Text = hang.Cells["SoLuongDia"].Value.ToString();
                cbNhaCungCap.SelectedValue = hang.Cells["MaNCC"].Value;
                txtGiaNhap.Text = hang.Cells["DonGiaNhap"].Value.ToString();
                txtGiaBan.Text = hang.Cells["DonGiaBan"].Value.ToString();
                dttNgayNhap.Value = Convert.ToDateTime(hang.Cells["NgapNhapHang"].Value);

            }
            else
            {
                txtTenDia.Text = "";
                cbLoaiDia.SelectedValue = "";
                txtSoLuong.Text = "0";
                cbNhaCungCap.SelectedValue = "";
                txtGiaNhap.Text = "0";
                txtGiaBan.Text = "0";
                dttNgayNhap.Value = DateTime.Now;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            GetData();
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            hang = dgrDanhSach.CurrentRow;
            GetData();
            Showthem(false);
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            hang = null;
            GetData();
            Showthem(true);
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (!expandablePanel3.Expanded)
                expandablePanel3.Expanded=true;
        }

        private void btnDSDia_Click(object sender, EventArgs e)
        {
            GetAll();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (cbChoose.SelectedIndex == 0)
                dgrDanhSach.DataSource = from dn in dt.vwDiaNhacs
                                         where dn.MaDia.ToString().Contains(txtSearch.Text)
                                         select dn;
            else
                dgrDanhSach.DataSource = from dn in dt.vwDiaNhacs
                                         where dn.TenDia.ToLower().Contains(txtSearch.Text.ToLower())
                                         select dn;
        }


    }
}
